<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="layui/css/layui.css">
    <script src="layui/layui.js"></script>
</head>

<!-- L2Dwidget.js L2D网页动画人物 -->
<script src="https://eqcn.ajz.miesnfu.com/wp-content/plugins/wp-3d-pony/live2dw/lib/L2Dwidget.min.js"></script>
<script>
    L2Dwidget.init({
        "model": {
            "jsonPath": "https://unpkg.com/live2d-widget-model-koharu@1.0.5/assets/koharu.model.json",
            "scale": 1,
            "hHeadPos": 0.5,
            "vHeadPos": 0.618
        },
        "display": {"position": "right", "width": 250, "height": 300, "hOffset": 0, "vOffset": 0},
        "mobile": {"show": true, "scale": 0.5},
        "react": {"opacityDefault": 0.7, "opacityOnHover": 0.2}
    });
</script>
<body>
<div style="margin: 0 43% ;color: #4E5465;font-size: 50px ">书籍信息</div>


<script type="text/html" id="titleTpl">
    {{#  if(d.state ==1 ){ }}
    <button class="layui-btn layui-btn-sm">上架</button>
    {{#  } else { }}
    <button class="layui-btn layui-btn-sm layui-btn-danger">下架</button>
    {{#  } }}
</script>

<div class="demoTable">
    <div class="layui-inline">
        <input class="layui-input" name="id" id="demoReload" autocomplete="off" placeholder="请输入书名">

    </div>
    <button class="layui-btn" data-type="reload">搜索</button>
</div>

<form class="layui-form " id="formData" style="display: none" lay-filter="formData">
    <input type="hidden" name="bookid">
    <div class="layui-form-item">
        <label class="layui-form-label">书名</label>
        <div class="layui-input-inline">
            <input type="text" name="bookname" placeholder="请输入" autocomplete="off" class="layui-input"
                   lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">作者</label>
        <div class="layui-input-inline">
            <input type="text" name="author" placeholder="请输入" autocomplete="off" class="layui-input"
                   lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">出版社</label>
        <div class="layui-input-inline">
            <input type="text" name="press" placeholder="请输入" autocomplete="off" class="layui-input"
                   lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">类别</label>
        <div class="layui-input-inline">
            <select name="typeid" id="typeid" lay-filter="aihao" lay-verify="required">
                <option value="0">请选择</option>
            </select>
        </div>
    </div>
    <div class="layui-upload">
        <button type="button" class="layui-btn" id="test2">多图片上传</button>
        <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
            <div class="layui-upload-list" id="demo2" style="height: 100px"></div>
        </blockquote>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">进价</label>
        <div class="layui-input-inline">
            <input type="number" name="inprice" placeholder="请输入" autocomplete="off" class="layui-input"
                   lay-verify="price">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">售价</label>
        <div class="layui-input-inline">
            <input type="number" name="outprice" placeholder="请输入" autocomplete="off" class="layui-input"
                   lay-verify="price">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否上架</label>
        <div class="layui-input-inline">
            <select name="state" lay-verify="required">
                <option value="">请选择</option>
                <option value="1">上架</option>
                <option value="0">下架</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <button class="layui-btn" id="btn" lay-submit="" lay-filter="submitForm">提交</button>
    </div>
</form>

<script type="text/html" id="url">
    {{#  if(d.url !=null){ }}
    <img src="{{ d.url }}" style="width:50px;height: 50px">
    {{#  } else { }}

    {{#  } }}
</script>


<script type="text/html" id="state">
    {{#  if(d.state ==0){ }}
    <button class="layui-btn layui-btn-sm">上架</button>
    {{#  } else { }}
    <button class="layui-btn layui-btn-sm layui-btn-danger">下架</button>
    {{#  } }}
</script>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addbook">新增</button>
    </div>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
</body>
<script>
    layui.use(['table', 'jquery', 'form', 'layer', 'upload'], function () {
        var table = layui.table;
        var $ = layui.jquery;
        var form = layui.form;
        var layer = layui.layer;
        var upload = layui.upload;

        $(function () {
            showSelect();
            showTable();
        });

        //监听表单提交事件
        form.on('submit(submitForm)', function (data) {
            $.ajax({
                type: 'post',
                url: '${pageContext.request.contextPath}/book/update',
                data: data.field,
                dateType: 'json',
                success: function (result) {
                    if (result.code == 1) {
                        layer.msg(result.msg, {icon: 1});
                        showtable();
                    } else {
                        layer.msg(result.msg, {icon: 5})
                    }
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        function showTable() {
            //第一个实例
            table.render({
                elem: '#test'
                , url: '${pageContext.request.contextPath}/book/query'
                , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示'
                    , layEvent: 'LAYTABLE_TIPS'
                    , icon: 'layui-icon-tips'
                }]
                , title: '用户数据表'
                , cols: [[
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'bookid', title: '书ID', fixed: 'left', unresize: true, sort: true}
                    , {field: 'bookname', title: '书名', edit: 'text'}
                    , {field: 'author', title: '作者', edit: 'text', sort: true}
                    , {field: 'count', title: '数量', edit: 'text'}
                    , {field: 'type', title: '类别'}
                    , {field: 'press', title: '出版社', sort: true}
                    , {field: 'state', title: '状态', sort: true, templet: '#titleTpl'}
                    , {field: 'outprice', title: '售价', sort: true}
                    , {field: 'inprice', title: '进价', sort: true}
                   , {
                        field: 'url', title: '主图', templet: '#url'
                    }
                    , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 180}
                ]]
                , page: true
                , id: 'testReload'
                , limit: 5
                , limits: [3, 5, 8, 10]
            });

        }


        //多图片上传
        upload.render({
            elem: '#test2'
            , url: '${pageContext.request.contextPath}/book/img' //改成您自己的上传接口
            , auto: false
            , bindAction: '#btn'
            , multiple: true
            , choose: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo2').append('<img src="' + result + '" alt="' + file.name + '" class="layui-upload-img" style="width: 50px;height: 50px">')
                });
            }
            , done: function (res) {
                layer.msg("图片上传成功")
            }
        });


        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选');
                    break;
                //新增
                case 'addbook':
                    //清空
                    $("#formData")[0].reset();
                    layer.open({
                        type: 1
                        , title: '新增书籍'
                        ,area:['500px','80%']
                        , content: $('#formData')
                        , anim: 1

                    });

                //自定义头工具栏右侧图标 - 提示
            }

        });

        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        type: 'post',
                        url: '${pageContext.request.contextPath}/book/del',
                        data: {bookid: data.bookid},
                        dataType: 'json',
                        success: function (result) {
                            if (result.code == 1) {
                                layer.msg(result.msg, {icon: 1});
                                showTable();
                            } else {
                                layer.msg(result.msg, {icon: 5});
                            }
                        }
                    });
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                form.val("formData", data);
                layer.open({
                    type: 1
                    , title: '修改书籍'
                    , content: $('#formData')
                    , anim: 1

                });
            }
        });

        function showSelect() {
            $.ajax({
                type: "get",
                url: '${pageContext.request.contextPath}/book/type',
                dataType: 'json',//根据需求分析是否需要写
                success: function (result) {
                    //alert(JSON.stringify(result));
                    for (var i = 0; i < result.length; i++) {
                        $("#typeid").append(
                            $("<option>").attr("value", result[i]['typeid']).text(result[i]['type'])
                        )
                    }
                    //alert($("#typeid").html())
                    //更新渲染下拉框数据
                    form.render('select', 'formData');
                }
            })
        }

        //表单提交
        form.on('submit(submitForm)', function (data) {
            $.ajax({
                type: 'post',
                url: '${pageContext.request.contextPath}/book/update',
                data: JSON.stringify(data.field),
                contentType: 'application/json;charset=utf-8',
                success: function (result) {
                    layer.closeAll();
                    if (result.code == 1) {
                        //layer.msg  提示框
                        showTable();
                        layer.msg(result.msg);
                    } else {
                        layer.msg(result.msg);
                    }

                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //搜索时执行表格重载方法
        var $ = layui.$, active = {
            reload: function () {
                var demoReload = $('#demoReload').val();
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        bookname: demoReload
                    }
                }, 'data');
            }
        };

        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        form.verify({
            price: [
                /^[+]{0,2}(\d+)$|^[+]{0,2}(\d+\.\d+)$/
                , '价格不能为负数'
            ]
        });

    });
</script>
</html>
